<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: ToDo.php 323 2008-12-01 18:32:53Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Common.Data.PortalToDoRecord');
Prado::using('Application.Common.Data.PortalUserRecord');

class ToDo extends PortalAdminPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->setAdminPageTitle('ToDo');
	}

	public function getDataRowsToDoByYou($offset, $rows)
	{
		$criteria							= new TActiveRecordCriteria;
		$criteria->Condition				= 'createdby = :createdby';
		$criteria->Parameters[':createdby']	= $this->User->Id;
		$criteria->OrdersBy['deadline']		= 'desc';
		$criteria->OrdersBy['priority']		= 'desc';
		$criteria->Limit					= $rows;
		$criteria->Offset					= $offset;
		return PortalToDoRecord::finder()->findAll($criteria);
	}

	protected function getRowCountToDoByYou()
    {
        return $this->DataAccess->createCommand("select count(id) from tbltodo where createdby={$this->User->id}")->queryScalar();
    }

	public function getDataRowsToDoToYou($offset, $rows)
	{
		$criteria								= new TActiveRecordCriteria;
		$criteria->Condition					= 'assignedto = :assignedto';
		$criteria->Parameters[':assignedto']	= $this->User->Id;
		$criteria->OrdersBy['deadline']			= 'desc';
		$criteria->OrdersBy['priority']			= 'desc';
		$criteria->Limit						= $rows;
		$criteria->Offset						= $offset;
		return PortalToDoRecord::finder()->findAll($criteria);
	}

	protected function getRowCountToDoToYou()
    {
        return $this->DataAccess->createCommand("select count(id) from tbltodo where assignedto={$this->User->id}")->queryScalar();
    }

	public function pageChangedToDoByYou($sender,$param)
    {
        // change the current page index to the new one
        $this->dlToDoListByYou->CurrentPageIndex	= $param->NewPageIndex;
        $this->dataBind();
    }

	public function pageChangedToDoToYou($sender,$param)
    {
        // change the current page index to the new one
        $this->dlToDoListToYou->CurrentPageIndex	= $param->NewPageIndex;
        $this->dataBind();
    }

	public function dataBind()
	{
		$toYouOffset						= $this->dlToDoListToYou->CurrentPageIndex * $this->dlToDoListToYou->PageSize;
		$toYouRows							= $this->dlToDoListToYou->PageSize;
		$this->dlToDoListToYou->DataSource	= $this->getDataRowsToDoToYou($toYouOffset,$toYouRows);
		$this->dlToDoListToYou->dataBind();

		$byYouOffset						= $this->dlToDoListByYou->CurrentPageIndex * $this->dlToDoListByYou->PageSize;
		$byYouRows							= $this->dlToDoListByYou->PageSize;
		$this->dlToDoListByYou->DataSource	= $this->getDataRowsToDoByYou($byYouOffset,$byYouRows);
		$this->dlToDoListByYou->dataBind();

		$this->ddlAssignedTo->DataSource	= $this->Users;
		$this->ddlAssignedTo->dataBind();
	}

	public function getUsers()
	{
		return PortalUserRecord::finder()->FindAll('role = ?' , $this->Application->Parameters['AdminRole']);
	}

	public function onInit($param)
	{
		parent::onInit($param);

		if(!$this->IsPostBack)
		{
			$this->dlToDoListByYou->VirtualItemCount	= $this->RowCountToDoByYou;
			$this->dlToDoListToYou->VirtualItemCount	= $this->RowCountToDoToYou;

			$this->dataBind();
		}
	}

	public function SaveToDo($sender, $param)
	{
		if($this->IsValid)
		{
			$todo				= new PortalToDoRecord();
			$todo->title		= $this->tbTitle->Text;
			$todo->description	= $this->tbDescription->Text;
			$todo->created		= time();
			$todo->createdby	= $this->User->id;
			$todo->deadline		= $this->dpDeadline->TimeStamp;
			$todo->priority		= $this->ddlPriority->SelectedValue;
			$todo->assignedto	= $this->ddlAssignedTo->SelectedValue;
			$todo->status		= $this->ddlStatus->SelectedValue;
			$todo->save();
		}

		$this->gotoPage('Admin.ToDo');
	}

	public function itemCommand($s,$p)
	{
	}

	public function editItem($sender,$param)
	{
		$item	= $param->Item;

		if($sender->Id === 'dlToDoListByYou')
		{
			$this->dlToDoListByYou->SelectedItemIndex	= -1;
			$this->dlToDoListByYou->EditItemIndex		= $param->Item->ItemIndex;
		}
		else if($sender->Id === 'dlToDoListToYou')
		{
			$this->dlToDoListToYou->SelectedItemIndex	= -1;
			$this->dlToDoListToYou->EditItemIndex		= $param->Item->ItemIndex;
		}

		$this->dataBind();
	}

	public function cancelItem($sender,$param)
	{
		$item	= $param->Item;

		if($sender->Id === 'dlToDoListByYou')
		{
			$this->dlToDoListByYou->SelectedItemIndex	= -1;
			$this->dlToDoListByYou->EditItemIndex		= -1;
		}
		else if($sender->Id === 'dlToDoListToYou')
		{
			$this->dlToDoListToYou->SelectedItemIndex	= -1;
			$this->dlToDoListToYou->EditItemIndex		= -1;
		}

		$this->dataBind();
	}

	public function updateItem($sender,$param)
	{
		$item	= $param->Item;

		if($sender->Id === 'dlToDoListByYou')
		{
			$id 	= $this->dlToDoListByYou->DataKeys[$item->ItemIndex];
			$todo	= PortalToDoRecord::finder()->findByid($id);
			$todo->title		= $item->edttbTitle->Text;
			$todo->description	= $item->edttbDescription->Text;
			$todo->deadline		= $item->edtdpDeadline->TimeStamp;
			$todo->priority		= $item->edtddlPriority->SelectedValue;
			$todo->assignedto	= $item->edtddlAssignedTo->SelectedValue;
			$todo->status		= $item->edtddlStatus->SelectedValue;
			$todo->save();

			$this->dlToDoListByYou->SelectedItemIndex	= -1;
			$this->dlToDoListByYou->EditItemIndex		= -1;
		}
		else if($sender->Id === 'dlToDoListToYou')
		{
			$this->dlToDoListToYou->SelectedItemIndex	= -1;
			$this->dlToDoListToYou->EditItemIndex		= -1;
		}

		$this->dataBind();
	}

	public function deleteItem($sender, $param)
	{
		if($sender->Id === 'dlToDoListByYou')
		{
			$id = $this->dlToDoListByYou->DataKeys[$param->Item->ItemIndex];
		}
		else if($sender->Id === 'dlToDoListToYou')
		{
			$id = $this->dlToDoListToYou->DataKeys[$param->Item->ItemIndex];
		}

		$todo = PortalToDoRecord::finder()->findByid($id);
		$todo->delete();
		$this->gotoPage('Admin.ToDo');
	}

	public function ValidateSelectedValue($value)
	{
		if($value !== 0 && $value !== '0' && $value !== '')
		{
			return $value;
		}
	}

	public function GetCssStyle($item)
	{
		switch($item->priority)
		{
			case 0:	return 'background-color:#FFFF80';break;
			case 1:	return 'background-color:#80FF80';break;
			case 2:	return 'background-color:#FF8080';break;
		}
	}
}
?>